//
//  XJCityListView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/9.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  城市列表view


#import "XJCityListView.h"
//cell

//model
#import "XJCityBigModel.h"

@interface XJCityListView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSMutableArray <XJCityBigModel *> *dataArray;
@property(nonatomic, strong) NSMutableArray *indexTitleArr;


@property(nonatomic, strong) UILabel *currentCityLabel; // 当前城市


@end

@implementation XJCityListView

#pragma mark  - 一 lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}


- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)indexTitleArr {
    if (!_indexTitleArr) {
        _indexTitleArr = [NSMutableArray array];
    }
    return _indexTitleArr;
}


- (UILabel *)currentCityLabel {
    if (!_currentCityLabel) {
        _currentCityLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, KWidth - 100, 44)];
        _currentCityLabel.text = @"杭州";
    }
    return _currentCityLabel;
}


#pragma mark  - 二 init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self setupView];
    }
    return self;
}

- (void)setupView {
    
    [self readLocalCityJSONData];
    
    self.tableView.frame = self.bounds;
    [self addSubview:self.tableView];

    [self setupCuttentCityView];
}

- (void)setupCuttentCityView {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 44)];
    
    // 当前城市
    UILabel *tipsLabel = [[UILabel alloc] init];
    tipsLabel.text = @"当前城市";
    //    tipsLabel.backgroundColor = [UIColor lightGrayColor];
    [headerView addSubview:tipsLabel];
    
    UIImageView *iconIV = [[UIImageView alloc] init];
    iconIV.image = [UIImage imageNamed:@"address_list_off"];
    [headerView addSubview:iconIV];
    //    iconIV.backgroundColor = [UIColor redColor];
    
    [headerView addSubview:self.currentCityLabel];
    //    self.currentCityLabel.backgroundColor = [UIColor yellowColor];
    
    self.tableView.tableHeaderView = headerView;
    
    // layout
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 44));
        make.left.equalTo(headerView.mas_left).offset(20);
        make.top.equalTo(headerView.mas_top);
    }];
    [iconIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(10, 15));
        make.left.equalTo(tipsLabel.mas_right).offset(5);
        make.centerY.equalTo(tipsLabel.mas_centerY);
    }];
    [self.currentCityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconIV.mas_right).offset(5);
        make.top.bottom.right.equalTo(headerView);
    }];
}


#pragma mark  - 三 代理方法 UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    XJCityBigModel *bigModel = self.dataArray[section];
    return  bigModel.addresses.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellId"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"UITableViewCellId"];
    }
    
    XJCityBigModel *bigModel = self.dataArray[indexPath.section];
    XJCityModel *smallModel = bigModel.addresses[indexPath.row];
    
    cell.textLabel.text = smallModel.name;
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.dataArray[section].firstChar;
}
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.indexTitleArr;
}


#pragma mark  - 三 代理方法 UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 将城市回到出去并返回。

    XJCityBigModel *bigModel = self.dataArray[indexPath.section];
    XJCityModel *cityModel = bigModel.addresses[indexPath.row];
    
    XJLog(@"%@",cityModel.name);
    
    
    if (self.xjcityListSelectCityBlock) {
        self.xjcityListSelectCityBlock(cityModel.name);
    }
}


#pragma mark  - 四 点击事件回调

#pragma mark - 五 Private  method
- (void)readLocalCityJSONData {
    // json数据.---->Data --->Dic/Arr/Str
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"addressCity" ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
    
    // 转字典
    NSArray *bigArr = [NSJSONSerialization JSONObjectWithData:jsonData options:(NSJSONReadingMutableContainers) error:nil];
    
    self.dataArray = [XJCityBigModel mj_objectArrayWithKeyValuesArray:bigArr];
    
    for (XJCityBigModel *bigModel in self.dataArray) {
        [self.indexTitleArr addObject:bigModel.firstChar];
    }
}



@end
